$(function () {
    //ajax设置
    //	$(document).ajaxStart(function(){
    //	  layer.load(0, {shade: false});
    //	});
    //	$(document).ajaxStop(function(){
    //	  layer.close(layer.load());
    //	});

    //下拉
   /* $(".dropdown").live('click', function (e) {
        if ($(this).hasClass("open")) {
            $(this).removeClass("open");
        } else {
            $(".dropdown").removeClass("open");
            $(this).addClass("open");
        }
        //e.stopPropagation();
    });*/
    //formhash
    try {
        // $('form').each(function(i, n) {
        //     $(this).append('<input type="hidden" name="formhash" value="' + formhash + '"/>');
        // })

        $("a").each(function () {
            var href = $(this).attr('href');
            if (href && href.indexOf('javascript:') == -1 && href.indexOf('formhash') == -1 && $(this).attr('rel') !=
                'nofollow') {
                if (href.indexOf('?') != -1) {
                    href += '&formhash=' + formhash;
                } else {
                    href += '?formhash=' + formhash;
                }
                $(this).attr('href', href);
            }
        })
    } catch (e) {
    }
    //按钮弹窗
    $(".add_user_open").live('click', function () {
        var panel = '' + $(this).attr('data-panel') + '';
        var callback = '' + $(this).attr('data-callback') + '';
        var title = $(this).attr('data-title');
        var btn = $(this).attr('data-btn');
        title = (typeof (title) == 'undefined') ? '信息' : title;
        btn = (typeof (btn) == 'undefined') ? ['确认', '取消'] : btn.split(",");
        //捕获页
        layer.open({
            type: 1,
            maxWidth: 600,
            title: title,
            content: $('.' + panel + ''), //捕获的元素
            btn: btn,
            yes: function (index, layero) {
                // eval(callback);·
                //若打开的是第一个输入密码页面
                if (panel == "add_user") {

                    var username=$("#username").val();
                    //如果用户名为空
                    removeUsernameText();
                    if (username==""){
                       $("#username").after("<span class='usernameText' style='color:red'>&nbsp;用户名不能为空</span>")
                    }
                    //如果用户名不为空
                    else{
                        var _panel = '' + $('.add_user_setup2').attr('data-panel') + '';
                        var _callback = '' + $('.add_user_setup2').attr('data-callback') + '';
                        var _title = $('.add_user_setup2').attr('data-title');
                        var _btn = $('.add_user_setup2').attr('data-btn');
                        _title = (typeof (_title) == 'undefined') ? '信息' : _title;
                        // btn = (typeof(btn)=='undefined')?['确认']:btn.split(",");
                        if(checkUsername()){
                            $("#add_user").submit(addUser(function (res) {
                                checkLogin(res);
                                //添加用户失败
                                if (res.meta.state == "failed") {
                                    var msg = res.meta.message;
                                    //console.log(res.meta.message);
                                    $("#username").after("<span style='color:red'>&nbsp;" + msg + "</span>")
                                }
                                //添加用户成功
                                else if (res.meta.state == "success") {
                                    layer.close(index);
                                    layer.open({
                                        type: 1,
                                        maxWidth: 600,
                                        title: _title,
                                        content: $('.' + _panel + ''), //捕获的元素
                                        btn: _btn,
                                        yes: function (index, layero) {
                                            console.log(res.meta.message);
                                            eval(_callback);
                                            window.location.reload();
                                        }
                                    });
                                }
                            }));
                        }
                    }
                }

            },
            cancel: function (index) {
                // layer.close(index);
            }
        });
        return false;
    })
    //确认弹窗
    $('.add_user_confirm').live('click', function () {
        var callback = '' + $(this).attr('callback') + '';
        layer.confirm('' + $(this).attr('msg') + '', {
            btn: ['确认', '取消'], //按钮
            icon: 3
        }, function () {
            try {
                eval(callback);
                layer.close(layer.confirm());
            } catch (e) {

            }
        });
    })
});
